﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ServerLib;

namespace ChatClient
{
    public partial class Registration : Form
    {
        private IRemoteServer remserver2;
        private bool LoginGood = false;
        private bool PasswordCorrect = false;
        private bool NameGood = false;
        private bool LastNameGood = false;
        private bool BirthDateGood = false;

        public Registration(IRemoteServer remserver)
        {
            InitializeComponent();
            remserver2 = remserver;
        }

        private void buttonRegister_Click(object sender, EventArgs e)
        {
            if (SecondaryFieldsCheck())
            {
                return;
            }
            string BirthDate;
            DateTime dt = dateTimePicker1.Value;
            BirthDate = dt.Year + "-" + dt.Month + "-" + dt.Day;

            remserver2.CreateUser(textBoxLogin.Text, textBoxPassword.Text);
            remserver2.SetUserInfo(textBoxLogin.Text, textBoxPassword.Text, textBoxName.Text,
                textBoxLastName.Text, BirthDate, radioButton1.Checked == true ? ClientInfo.EnumSex.Male :
                ClientInfo.EnumSex.Female);
            MessageBox.Show("New user was sucsessfuly registred");
            DialogResult = System.Windows.Forms.DialogResult.OK;
            UseWaitCursor = true;
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void textBoxLogin_MouseLeave(object sender, EventArgs e)
        {
            labelLoginCheck.Visible = false;
            errorProvider1.Clear();
            if (remserver2.CheckLogin(textBoxLogin.Text))
            {
                if (textBoxLogin.Text != "".TrimEnd())
                {
                    labelLoginCheck.Visible = true;
                    labelLoginCheck.Text = "This login is free";
                }
                LoginGood = true;
            }
            else
            {
                errorProvider1.SetError(textBoxLogin, "Such login exists");
                LoginGood = false;
            }
            CheckRegistrationButton();
        }

        private void checkBoxPassword_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxPassword.Checked)
            {
                textBoxPassword.UseSystemPasswordChar = false;
                textBoxConfirm.UseSystemPasswordChar = false;
            }
            else
            {
                textBoxPassword.UseSystemPasswordChar = true;
                textBoxConfirm.UseSystemPasswordChar = true;
            }
        }
        private void textBoxConfirm_Leave(object sender, EventArgs e)
        {
            errorProvider1.Clear();
            labelConfirmCheck.Visible = false;
            PasswordConfirmatiom();
        }

        private void PasswordConfirmatiom()
        {
            if (textBoxConfirm.Text.TrimEnd() != "" && textBoxPassword.Text == textBoxConfirm.Text)
            {
                labelConfirmCheck.Visible = true;
                labelConfirmCheck.Text = "Password is confirmed";
                PasswordCorrect = true;
            }
            else
                if (textBoxPassword.Text != "")
                {
                    errorProvider1.SetError(textBoxConfirm, "Passwod is not confirmed");
                }
            CheckRegistrationButton();
        }

        private void CheckRegistrationButton()
        {
            if (textBoxLogin.Text != null && PasswordCorrect && LoginGood && NameGood)
            {
                buttonRegister.Enabled = true;
            }
        }

        private void textBoxName_Leave(object sender, EventArgs e)
        {
            labelNameCheck.Visible = false;
            if (textBoxName.Text.TrimEnd() != "")
            {
                NameGood = true;
                labelNameCheck.Visible = true;
            }
            else
            {
                NameGood = false;
            }
            CheckRegistrationButton();
        }

        private bool SecondaryFieldsCheck()
        {
            if (LastNameGood && BirthDateGood)
            {
                return false;
            }
            else
            {
                RegistrationConfiramation rc = new RegistrationConfiramation();
                rc.ShowDialog();
                if (rc.DialogResult == System.Windows.Forms.DialogResult.Cancel)
                    return true;
            }
            return false;
        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            BirthDateGood = true;
        }

        private void textBoxLastName_TextChanged(object sender, EventArgs e)
        {
            LastNameGood = true;
        }
    }
}
